<?php defined('SYSPATH') or die('No direct script access.');
//分布式配置
class Controller_Distribute extends Stourweb_Controller
{

    private $parentkey = null;
    private $itemid = null;


    public function before()
    {
        parent::before();

        $this->assign('parentkey', $this->params['parentkey']);
        $this->assign('itemid', $this->params['itemid']);
        $this->parentkey = $this->params['parentkey'];
        $this->itemid = $this->params['itemid'];
        $this->assign('helpico', Common::getIco('help'));


    }

    /*
     * 分布式配置,数据库主从配置
     * */


    public function action_database()
    {
        $configinfo = ORM::factory('sysconfig')->getConfig(0);
        $this->assign('config', $configinfo);
        $this->display('stourtravel/distribute/database');
    }

    public function action_ajax_save_database()
    {
        $model = new Model_Sysconfig();
        $flag = $model->saveConfig($_POST);
        //单独写配置文件到data目录
        if($flag)
        {
            $filename = SLINEDATA.'/slave.config.php';
            unset($_POST['webid']);
            file_put_contents($filename, '<?php defined(\'SYSPATH\') or die(\'No direct script access.\');' . PHP_EOL . 'return ' . var_export($_POST, true) . ';');

        }
        echo json_encode(array('status' => $flag));

    }


    /*
     * 分布式配置,session配置
     * */
    public function action_session()
    {
        $configinfo = ORM::factory('sysconfig')->getConfig(0);
        $this->assign('config', $configinfo);
        $this->display('stourtravel/distribute/session');

    }

    /**
     * session配置保存
     */
    public function action_ajax_save_session()
    {
        $cfg_session_driver = $_POST['cfg_session_driver'];

        //如果配置的是redis,检测php里是否配置了redis组件.
        if($cfg_session_driver == 'redis')
        {
            if(!class_exists('redis'))
            {
                echo json_encode(array('status' => false,'msg'=>'当前php环境未开启redis,请开启后再试'));
                exit;
            }
            try{
                $redis = new Redis();
                $redis->connect($_POST['cfg_session_redis_host'], $_POST['cfg_session_redis_port']); //连接Redis
                if($_POST['cfg_session_redis_password'])
                {
                    $redis->auth($_POST['cfg_session_redis_password']); //密码验证
                }
                $redis->close();
            }catch (Exception $ex){
                echo json_encode(array('status' => false,'msg'=>'redis连接失败,请检查账号信息是否正确'));
                exit;
            }
        }
        $model = new Model_Sysconfig();
        $flag = $model->saveConfig($_POST);
        //单独写配置文件到data目录
        if($flag)
        {
            $filename = SLINEDATA.'/redis.config.php';
            unset($_POST['webid']);
            file_put_contents($filename, '<?php defined(\'SYSPATH\') or die(\'No direct script access.\');' . PHP_EOL . 'return ' . var_export($_POST, true) . ';');

        }
        echo json_encode(array('status' => $flag));
    }

	/**
	 * 队列开关配置
	 * User: netman
	 * Date: 2023/7/21 10:37
	 */
	public function action_queue()
	{
		$configinfo = ORM::factory('sysconfig')->getConfig(0);
		$this->assign('config', $configinfo);
		$this->display('stourtravel/distribute/queue');
	}



}